package com.billsharing.server;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject.*;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
/**
 * Servlet implementation class Home
 */
@WebServlet("/Home")
public class Home extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    int uid;
    String result;
    Connection conn;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	
	{
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		response.setContentType("text/x-json;charset=UTF-8");
		//System.out.println(request.getParameter("uid"));
		uid = Integer.parseInt(request.getParameter("uid"));
		System.out.println("User id in home page is"+request.getParameter("uid"));
		try
		{
		DbConnect dbconn = new DbConnect();
		conn = dbconn.getConnection();
		Statement s=conn.createStatement();
		s.executeQuery("select fndemail,credit,debit from Friend where userid="+uid);
		ResultSet rs = s.getResultSet();
		JSONArray array=new JSONArray();
		while(rs.next())
		{
			
			  JSONObject obj=new JSONObject();
	           obj.put("email", rs.getString("fndemail"));
	           obj.put("credit",rs.getString("credit"));
	           obj.put("debit",rs.getString("debit"));
	           array.add(obj);
	           for(int i=0;i<array.size();i++)
	           {
	        	   JSONObject jobj = array.getJSONObject(i);
	        	  System.out.println(jobj.getString("email")+jobj.getDouble("credit")+jobj.getDouble("debit"));
	           }
	          
		}
		
		  
			System.out.println(array);
			out.print(array);
		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
